home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pcmagazi / 1992 / 04 / demo2.c < prev    next >
Text File  |  1991-11-26  |  8KB  |  197 lines

  1. // DEMO2.C  Minimally pen-aware Windows application that demonstrates 
  2. // simple text input or handwritten input via a dialog, then displays 
  3. // same text in main window.
  4. // Copyright (C) 1991 Ray Duncan
  5.  
  6. #define WIN31
  7.  
  8. #include "windows.h"
  9. #include "penwin.h"
  10. #include "demo2.h"
  11.  
  12. HANDLE hInst;                               // instance handle
  13. HWND hWnd;                                  // main window handle
  14.  
  15. HANDLE hPenWin = NULL;                      // Pen Windows module handle
  16.  
  17. #define INPUTSIZE 80                        // max length of input
  18. #define OUTPUTSIZE 256                      // max length of output
  19.  
  20. char buf1[INPUTSIZE];                       // user's input buffer
  21. char buf2[OUTPUTSIZE];                      // formatted output buffer
  22.  
  23. //
  24. // WinMain - entry point from Windows.  Registers window class,
  25. // creates main window, processes messages until WM_QUIT received.
  26. //
  27. int PASCAL WinMain(HANDLE hInstance, 
  28.     HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  29. {
  30.     MSG msg;                     
  31.  
  32.     VOID (FAR PASCAL *RegisterPenApp)(WORD, BOOL) = NULL;
  33.  
  34.     hInst = hInstance;                      // save this instance handle
  35.  
  36.     strcpy(buf1, "Nothing yet!");           // initialize output text
  37.  
  38.     if(!hPrevInstance)                      // if first instance,
  39.         if(!InitApplication(hInstance))     // register window class
  40.             return (FALSE);                 // exit if couldn't register
  41.  
  42.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  43.         return (FALSE);
  44.  
  45.     if(hPenWin = GetSystemMetrics(SM_PENWINDOWS))   // Pen Windows running?
  46.     {                                       // if so, register as Pen app
  47.         if(RegisterPenApp = GetProcAddress(hPenWin, "RegisterPenApp"))
  48.             (*RegisterPenApp)(RPA_DEFAULT, TRUE);
  49.     }
  50.  
  51.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  52.     {
  53.         TranslateMessage(&msg);             // translate virtual key codes
  54.         DispatchMessage(&msg);              // dispatch message to window
  55.     }
  56.     return (msg.wParam);                    // return code = WM_QUIT value
  57. }
  58.  
  59. //
  60. //  InitApplication - registers window class for this application
  61. //
  62. BOOL InitApplication(HANDLE hInstance)
  63. {
  64.     WNDCLASS  wc;
  65.  
  66.                                             // set class parameters
  67.     wc.style = NULL;                        // class style
  68.     wc.lpfnWndProc = MainWndProc;           // class callback function
  69.     wc.cbClsExtra = 0;                      // extra per-class data
  70.     wc.cbWndExtra = 0;                      // extra per-window data
  71.     wc.hInstance = hInstance;               // handle of class owner
  72.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);     // default icon
  73.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  74.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  75.     wc.lpszMenuName =  "DemoMenu";          // name of menu resource
  76.     wc.lpszClassName = "DemoWinClass";      // name of window class
  77.  
  78.     return (RegisterClass(&wc));            // register class, return flag
  79. }
  80.  
  81. //
  82. // InitInstance - creates main window for this application instance
  83. //
  84. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  85. {
  86.     hWnd = CreateWindow(                    // create frame window
  87.         "DemoWinClass",                     // window class name
  88.         "Pen Windows Demo #2",              // text for title bar
  89.         WS_OVERLAPPEDWINDOW,                // window style
  90.         CW_USEDEFAULT,                      // default horizontal position
  91.         CW_USEDEFAULT,                      // default vertical position
  92.         CW_USEDEFAULT,                      // default width
  93.         CW_USEDEFAULT,                      // default height
  94.         NULL,                               // no parent window
  95.         NULL,                               // use class default menu
  96.         hInstance,                          // window owner
  97.         NULL                                // unused pointer
  98.     );
  99.  
  100.     if (!hWnd) return (FALSE);              // error, can't create window
  101.  
  102.     ShowWindow(hWnd, nCmdShow);             // make window visible
  103.     UpdateWindow(hWnd);                     // force WM_PAINT message
  104.     return (TRUE);                          // return success flag
  105. }
  106.  
  107. //
  108. // MainWndProc - callback function for main application window
  109. //
  110. long FAR PASCAL MainWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  111. {
  112.     HDC hdc;                                // device context handle
  113.     PAINTSTRUCT ps;                         // painting info structure
  114.     RECT rect;                              // receives client rectangle
  115.  
  116.     switch (wMsg) 
  117.     {
  118.         case WM_COMMAND:                    // menu command received,
  119.             DoCommand(hWnd, wParam);
  120.             break;
  121.  
  122.         case WM_PAINT:                      // window needs repainting
  123.             hdc = BeginPaint(hWnd, &ps);    // get device context
  124.             GetClientRect(hWnd, &rect);     // get client rectangle
  125.             wsprintf(buf2, "You entered: %s", (LPSTR) buf1);
  126.             DrawText(hdc, buf2, -1, &rect,  // display formatted text
  127.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  128.             EndPaint(hWnd, &ps);            // release device context
  129.             break;
  130.  
  131.         case WM_SIZE:                       // window resized
  132.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  133.             UpdateWindow(hWnd);             // force WM_PAINT message
  134.             break;
  135.  
  136.         case WM_DESTROY:                    // window being destroyed
  137.             PostQuitMessage(0);
  138.             break;
  139.  
  140.         default:                            // let Windows handle it
  141.             return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  142.     }
  143.  
  144.     return(0);
  145. }
  146.  
  147. //
  148. // DoCommand - handles menu command messages
  149. //
  150. void DoCommand(HWND hWnd, WORD wParam)
  151. {
  152.     FARPROC lpProc;                         // far pointer to callback 
  153.  
  154.     switch(wParam)                          // decode it
  155.     {
  156.         case IDM_EXIT:                      // user picked File-Quit
  157.             SendMessage (hWnd, WM_CLOSE, 0, 0L);
  158.             break;
  159.  
  160.         case IDM_ENTER:                     // user picked Edit-Enter
  161.             lpProc = MakeProcInstance(EntryDlgProc, hInst);
  162.             DialogBox(hInst, "TextEntryBox", hWnd, lpProc);
  163.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  164.             UpdateWindow(hWnd);             // force WM_PAINT message
  165.             FreeProcInstance(lpProc);
  166.             break;
  167.  
  168.         default:                            // unknown command, ignore it
  169.             break;
  170.     }
  171. }
  172.  
  173. //
  174. // EntryDlgProc - callback function for "Text Entry..." dialog
  175. //
  176. BOOL FAR PASCAL EntryDlgProc(HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  177. {
  178.     switch (wMsg) 
  179.     {
  180.         case WM_INITDIALOG:                 // dialog box initialization
  181.             return (TRUE);                  // signal message processed
  182.  
  183.         case WM_COMMAND:                    // command received
  184.             if(wParam == IDOK)              // OK button clicked?
  185.             {                               // yes, retrieve text
  186.                 GetDlgItemText(hDlg, IDD_EDITTEXT, buf1, INPUTSIZE-1);
  187.                 EndDialog(hDlg, TRUE);      // signal text accepted
  188.             }
  189.             else if(wParam == IDD_CANCEL)   // Cancel button clicked?
  190.             {
  191.                 strcpy(buf1, "Input was cancelled!");
  192.                 EndDialog(hDlg, FALSE);     // signal text not accepted
  193.             }
  194.     }
  195.     return(FALSE);                          // signal message not processed
  196. }
  197.